import re
import sys


def load_class(clspath):
    # cls_name = "venusdemilo.customizers"
    try:
        dot = clspath.rindex('.')
    except ValueError:
        raise ValueError("Error loading object '%s': not a full path" % clspath)

    module_path, name = clspath[:dot], clspath[dot + 1:]

    mod = load_module(module_path)

    try:
        obj = getattr(mod, name)
    except AttributeError:
        raise NameError("Module '%s' doesn't define any object named '%s'" % (mod, name))

    return obj


def load_module(modulepath):
    __import__(modulepath)
    mod = sys.modules[modulepath]
    return mod


def match_kv(text_to_match, dict):
    for (k, v) in dict.items():
        if re.match(k, text_to_match):
            cls_path = v
            return v
    if dict.has_key('default'):
        return dict['default']